package com.kmxd.ams.app.arrange.command;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kmxd.ams.client.archive.IArchiveOrderFieldSvc;
import com.kmxd.ams.client.archive.dto.ArchiveOrderFieldQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveOrderFieldListVO;
import com.kmxd.ams.core.common.enums.FieldOrderTypeEnum;
import com.kmxd.ams.infra.arrange.base.SystemBO;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 档案通用查询操作
 *
 * @author nickbi
 */
@Component
@AllArgsConstructor
public class CommonQueryCmd {
  private final IArchiveOrderFieldSvc archiveOrderFieldSvc;

  /**
   * 通用排序设置
   *
   * @param req
   * @param wrapper
   */
  public void buildFieldOrderQuery(SystemBO req, QueryWrapper<? extends SystemBO> wrapper) {
    ArchiveOrderFieldQueryDTO qryOrder = new ArchiveOrderFieldQueryDTO();
    qryOrder.setArchTypeId(req.getArchTypeId());
    qryOrder.setTableId(req.getTableId());
    List<ArchiveOrderFieldListVO> orderFieldList = archiveOrderFieldSvc.selectList(qryOrder);
    orderFieldList.forEach(
        item -> {
          String orderFieldCode = item.getOrderFieldCode();
          wrapper.orderBy(
              ObjectUtil.isNotEmpty(orderFieldCode),
              FieldOrderTypeEnum.ASC.getType().equals(item.getOrderType()),
              orderFieldCode);
        });
  }
}
